﻿using Castle.DynamicProxy;
using Microsoft.Extensions.Logging;
using System.Linq;
using System;
using System.Text;
using System.Reflection;

namespace MDT.LifeSewagePlatform.Conmmon
{
    public class CustomAutofacAop : IInterceptor
    {
        [Autowired]
        private ILogger<CustomAutofacAop> _logger { get; set; }
        public CustomAutofacAop()
        {
        }
        public void Intercept(IInvocation invocation)
        {
            StringBuilder argSbInfo = new StringBuilder($"\t\tTime{DateTime.Now} \n\t\tThe location where the request is processed:{invocation.TargetType.FullName},{invocation.Method.Name}\n");

            ParameterInfo[] parameterInfos = invocation.Method.GetParameters();
            object[] arguments = invocation.Arguments;
            for (int i = 0; i <parameterInfos.Length;i++)
            {
                Type type = arguments[i].GetType();

                argSbInfo.Append($"\t\tArgument:{type.FullName};");
                if (type.IsClass && type != typeof(string))
                {
                    foreach (var prop in type.GetProperties())
                    {
                        argSbInfo.Append($"\n\t\t{prop.Name}:{prop.GetValue(arguments[i])};");
                    }
                }
                else
                {
                    argSbInfo.Append($"\n\t\t{parameterInfos[i].Name}:{arguments[i]}");
                }
            }
            _logger.LogInformation(argSbInfo.ToString());
            invocation.Proceed();
        }
    }
}
